package com.zide.platform.controller;

import com.zide.common.core.domain.CommonResult;
import com.zide.common.core.domain.PageResult;
import com.zide.common.enums.BusinessType;
import com.zide.file.utils.poi.ExcelUtil;
import com.zide.platform.entity.PlatformPost;
import com.zide.platform.service.PlatformPostService;
import com.zide.platform.vo.search.PlatformPostSearchVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 石添添
 * @description
 * @date 2024-09-23 21:53
 */
@Tag(name = "后台管理 -岗位告管理")
@RestController
@RequestMapping("/platform/post")
@RequiredArgsConstructor
public class PlatformPostController {

    private final PlatformPostService postService;

    /**
     * 获取岗位列表
     */
    @GetMapping("/page")
    public CommonResult list(PlatformPostSearchVO searchVO) {
        PageResult<PlatformPost> result = postService.selectPage(searchVO);
        return CommonResult.success().put("data",result);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, PlatformPost post) {
        List<PlatformPost> list = postService.selectPostList(post);
        ExcelUtil<PlatformPost> util = new ExcelUtil<PlatformPost>(PlatformPost.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @GetMapping(value = "/{postId}")
    public CommonResult getInfo(@PathVariable Long postId) {
        return CommonResult.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PostMapping
    public CommonResult add(@Validated @RequestBody PlatformPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return CommonResult.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return CommonResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return CommonResult.success(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PutMapping
    public CommonResult edit(@Validated @RequestBody PlatformPost post) {
        if (!postService.checkPostNameUnique(post)) {
            return CommonResult.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            return CommonResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return CommonResult.success(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @DeleteMapping("/{postIds}")
    public CommonResult remove(@PathVariable Long[] postIds) {
        return CommonResult.success(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public CommonResult optionselect() {
        List<PlatformPost> posts = postService.selectPostAll();
        return CommonResult.success(posts);
    }
}
